package com.marver.sys.service;

import com.marver.common.dao.BaseDAOImpl;
import com.marver.entity.sys.UserRole;
import com.marver.sys.mapper.UserRoleMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @description: 用户角色service
 * @Author: Marver
 * @date: 2018-04-27 17:45
 **/
@Service
public class UserRoleService extends BaseDAOImpl {
    @Autowired
    private UserRoleMapper userRoleMapper;

    public void saveOrUpdate(Long userId , List<Long> roleIdList){
        //删除用户与角色关联关系
        userRoleMapper.deleteByExample(new HashMap<>().put("user_id", userId));

        if(CollectionUtils.isEmpty(roleIdList)){
            return;
        }
        //保存用户与角色关联关系
        List<UserRole> list = new ArrayList<>(roleIdList.size());
        roleIdList.forEach(e->{
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(e);
            list.add(userRole);
        });
        userRoleMapper.insertList(list);
    }

    /**
     * 用户对应角色的id列表
     * @param userId
     * @return
     */
    public List<Long> queryUserRoleIdList(Long userId){
        return (List<Long>)this.listBySqlMap("userRole.queryUserRoleIdList",userId);
    }
}
